//
//  ShowModelViewController.m
//  ElectronicSandTable
//
//  Created by 苏晓凯 on 2017/6/16.
//  Copyright © 2017年 苏晓凯. All rights reserved.
//

#import "ShowModelViewController.h"
#import "AirSurfaceView.h"
#import "AirSegmentKey.h"
#import "AirSurfaceRender.h"
#import "AppDelegate.h"
#import "SubAirSurfaceView.h"
#import "SubAirSurfaceView2.h"
#import "SubAirSurfaceView3.h"
#import "SubAirSurfaceView4.h"
#import "SubAirSurfaceView5.h"

/**
 *  屏幕宽度
 */
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
/**
 *  屏幕高度
 */
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ShowModelViewController ()
@property (nonatomic, strong) AirSurfaceView        * airSurfaceView;


@end

@implementation ShowModelViewController
//@synthesize airSurfaceView;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 实例化控件
//    self.airSurfaceView = [[AirSurfaceView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];
////        {
////        // 以下为air3DView的默认设置，可以不写
////        // 设置合适的裁剪 模型为unPixel像素值的时候被裁剪掉
////        [self.airSurfaceView.airSurfaceRender setFramerate:true unPixel:10];
////        // 取消边线加强，减轻负荷
////        [self.airSurfaceView.airSurfaceRender setEffectsFastSilhouette:false];
////    }
//    // 显示摇杆
//    [self.airSurfaceView showStick];
//    // 显示工具栏
//    [self.airSurfaceView showToolsBar];
//    [self.view addSubview:self.airSurfaceView];
//    [self.airSurfaceView.airSurfaceRender refresh];
    switch (self.index) {
        case 0:
        {
            [SubAirSurfaceView shareInstance:self.fileName view:self.view];
        }
            break;
        case 1:
        {
            [SubAirSurfaceView2 shareInstance:self.fileName view:self.view];
        }
            break;
        case 2:
        {
            [SubAirSurfaceView3 shareInstance:self.fileName view:self.view];
        }
            break;
        case 3:
        {
            [SubAirSurfaceView4 shareInstance:self.fileName view:self.view];
        }
            break;
        case 4:
        {
            [SubAirSurfaceView5 shareInstance:self.fileName view:self.view];
        }
            break;
        default:
            break;
    }

    
    //帧数测试按钮
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(testPerformance) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"帧数测试" forState:UIControlStateNormal];
    btn.frame = CGRectMake(ScreenHeight-90, 10, 80, 50);
    
    //返回按钮
    UIButton * btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btnBack];
    [btnBack addTarget:self action:@selector(btnBack) forControlEvents:UIControlEventTouchUpInside];
    [btnBack setImage:[UIImage imageNamed:@"icon_return"] forState:UIControlStateNormal];
    btnBack.frame = CGRectMake(0, 20, 40, 40);
    
   
    
    //加载模型

    

    // Do any additional setup after loading the view from its nib.
}

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)btnBack{
//    [self.airSurfaceView.airSurfaceRender refresh];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
/**
 *  性能初步测试
 */
- (void)testPerformance {
    if (self.airSurfaceView.airSurfaceRender.loadedGuidArray.count > 0) {
        [[self.airSurfaceView airSurfaceRender] testPerformance];
    }
}

-(void)showModel{
    
    NSString * guid = self.fileName;
    NSString * file = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:guid];
    NSLog(@"guid = %@",guid);
    NSLog(@"file = %@",file);
   //文件转换
    const char* path = [file cStringUsingEncoding:NSASCIIStringEncoding];
    const char* fileName = [file cStringUsingEncoding:NSASCIIStringEncoding];
    int result = [AirSurfaceRender convertBigGDfile:fileName tempPath:path];
    NSLog(@"%d",result);
    
    NSString *newFile = [[NSString alloc]initWithCString:path encoding:NSASCIIStringEncoding];
    NSLog(@"%@",newFile);
    BOOL isModelImported = [[[self.airSurfaceView airSurfaceRender] loadedGuidArray] containsObject:[guid stringByDeletingPathExtension]];
    UIActivityIndicatorView * activityIndicatorView=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    activityIndicatorView.center=self.view.center;
    [activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [activityIndicatorView setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:activityIndicatorView];
    [activityIndicatorView startAnimating];
    if (isModelImported) {
        [self.airSurfaceView.airSurfaceRender unloadModelFile:[guid stringByDeletingPathExtension] completion:^(BOOL finished) {
            if (finished) {
                NSLog(@"unloadModelFile OK");
                [activityIndicatorView stopAnimating];

                //            [[airSurfaceView airSurfaceRender] setViewMode:ViewModeIosmetric];
            } else {
                NSLog(@"unloadModelFile error");
            }
        }];
    }else{
        [self.airSurfaceView.airSurfaceRender loadModelFile:newFile waitUntilDone:YES completion:^(BOOL finished) {
            if (finished) {
                NSLog(@"loadModelFile OK");
                [activityIndicatorView stopAnimating];

                //                [[airSurfaceView airSurfaceRender] setViewMode:ViewModeIosmetric];
            } else {
                NSLog(@"loadModelFile error");
            }
        }];
    }
}



@end
